package com.lh_travel.lohas.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lh_travel.lohas.R;
import com.lh_travel.lohas.adapter.CommonAdapter;
import com.lh_travel.lohas.adapter.ViewHolder;
import com.lh_travel.lohas.base.api;
import com.lh_travel.lohas.base.baseActivity;
import com.lh_travel.lohas.domain.Comment;
import com.lh_travel.lohas.domain.DafultMessageEvent;
import com.lh_travel.lohas.domain.FilterMessage;
import com.lh_travel.lohas.domain.HotelFilterBean;
import com.lh_travel.lohas.widget.AutoScaleCheckTextView;
import com.lh_travel.lohas.widget.HeadGridView;
import com.tamic.novate.Throwable;
import com.tamic.novate.callback.RxResultCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * Created by aaron on 2018/6/20.
 */

public class HotelFilterActivity extends baseActivity {

    private HeadGridView gv_theme;
    private HeadGridView gv_star;
    private HeadGridView gv_price;
    private HeadGridView gv_score;
    private CommonAdapter scoreAdapter;

    private String scorePostion = "";
    private String pricePostion = "";
    private String starPostion = "";
    private String themePostion = "";
    private String position ="";
    private RelativeLayout rl_position;
    private TextView tv_position;
    private ImageButton img_back;
    private Button btn_confirm;
    private HotelFilterBean filterBean;
    private CommonAdapter priceAdapter;
    private CommonAdapter starAdapter;
    private CommonAdapter themeAdapter;
    private TextView tv_clear;
    private LinearLayout ll_position;
    private String id = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_hotel_filter);

        EventBus.getDefault().register(this);

        findView();
        bindListner();
        ensureUI();
    }

    @Override
    public void findView() {
        gv_theme = (HeadGridView) findViewById(R.id.gv_theme);
        gv_star = (HeadGridView) findViewById(R.id.gv_star);
        gv_price = (HeadGridView) findViewById(R.id.gv_price);
        gv_score = (HeadGridView) findViewById(R.id.gv_score);
        rl_position = (RelativeLayout) findViewById(R.id.rl_position);
        tv_position = (TextView) findViewById(R.id.tv_position);
        img_back = (ImageButton) findViewById(R.id.img_back);
        btn_confirm = (Button) findViewById(R.id.btn_confirm);
        tv_clear = (TextView) findViewById(R.id.tv_clear);
        ll_position = (LinearLayout) findViewById(R.id.ll_position);
    }

    @Override
    public void bindListner() {
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        rl_position.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(mContext, PositionFilterActivity.class);

                intent.putExtra("detail", filterBean);

                startActivity(intent);
            }
        });

        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                FilterMessage message = new FilterMessage();

                message.TAG = filterBean.id;
                message.pricePostion = (pricePostion.length() > 0 ) ? pricePostion.substring(0, pricePostion.length() -1) : pricePostion;
                message.scorePostion = (scorePostion.length() > 0 ) ? scorePostion.substring(0, scorePostion.length() -1) : scorePostion;
                message.starPostion = (starPostion.length() > 0 ) ? starPostion.substring(0, starPostion.length() -1) : starPostion;
                message.themePostion = (themePostion.length() > 0 ) ? themePostion.substring(0, themePostion.length() -1) : themePostion;
                message.position = id;
                message.bean = filterBean;

                EventBus.getDefault().post(message);

                finish();

            }
        });

        tv_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                position ="";
                tv_position.setText(position);
            }
        });
    }

    @Override
    public void ensureUI() {

        filterBean = (HotelFilterBean) getIntent().getSerializableExtra("detail");

        ll_position.setVisibility(filterBean.position_station.size() == 0 && filterBean.position_scenery.size() == 0 ? View.GONE : View.VISIBLE);

        initGrid();
    }

    private void initGrid() {

        themeAdapter = new CommonAdapter<Comment>(mContext, filterBean.theme, R.layout.item_filter) {
            @Override
            public void convert(ViewHolder helper, final Comment item, int position) {

                helper.setText(R.id.atv_item, item.title);

                AutoScaleCheckTextView autoView = helper.getView(R.id.atv_item);

                if (item.check){
                    autoView.setTextColor(Color.parseColor("#ff7d1f"));

                }else {
                    autoView.setTextColor(Color.parseColor("#595959"));
                }

                helper.setViewOnclick(R.id.atv_item, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (item.check){

                            themePostion = themePostion.replace(item.id + ",", "");

                        }else {

                            themePostion = themePostion + item.id + ",";

                        }

                        item.check = !item.check;

                        themeAdapter.notifyDataSetChanged();

                    }
                });
            }
        };

        starAdapter = new CommonAdapter<Comment>(mContext, filterBean.star, R.layout.item_filter) {
            @Override
            public void convert(ViewHolder helper, final Comment item, int position) {

                helper.setText(R.id.atv_item, item.title);
                AutoScaleCheckTextView autoView = helper.getView(R.id.atv_item);

                if (item.check){
                    autoView.setTextColor(Color.parseColor("#ff7d1f"));

                }else {
                    autoView.setTextColor(Color.parseColor("#595959"));
                }

                helper.setViewOnclick(R.id.atv_item, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (item.check){

                            starPostion = starPostion.replace(item.id + ",", "");

                        }else {

                            starPostion = starPostion + item.id + ",";

                        }

                        item.check = !item.check;

                        starAdapter.notifyDataSetChanged();

                    }
                });
            }
        };

        priceAdapter = new CommonAdapter<Comment>(mContext, filterBean.price_type, R.layout.item_filter) {
            @Override
            public void convert(ViewHolder helper, final Comment item, int position) {

                helper.setText(R.id.atv_item, item.title);

                AutoScaleCheckTextView autoView = helper.getView(R.id.atv_item);

                if (item.check){
                    autoView.setTextColor(Color.parseColor("#ff7d1f"));

                }else {
                    autoView.setTextColor(Color.parseColor("#595959"));
                }

                helper.setViewOnclick(R.id.atv_item, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (item.check){

                            pricePostion = pricePostion.replace(item.id + ",", "");

                        }else {

                            pricePostion = pricePostion + item.id + ",";

                        }

                        item.check = !item.check;

                        priceAdapter.notifyDataSetChanged();

                    }
                });
            }
        };

        scoreAdapter = new CommonAdapter<Comment>(mContext, filterBean.sore_type, R.layout.item_filter) {
            @Override
            public void convert(final ViewHolder helper, final Comment item, int position) {

                helper.setText(R.id.atv_item, item.title);

                AutoScaleCheckTextView autoView = helper.getView(R.id.atv_item);

                if (item.check){
                    autoView.setTextColor(Color.parseColor("#ff7d1f"));

                }else {
                    autoView.setTextColor(Color.parseColor("#595959"));
                }

                helper.setViewOnclick(R.id.atv_item, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (item.check){

                            scorePostion = scorePostion.replace(item.id + ",", "");

                        }else {

                            scorePostion = scorePostion + item.id + ",";

                        }

                        item.check = !item.check;

                        scoreAdapter.notifyDataSetChanged();

                    }
                });
            }
        };

        gv_theme.setAdapter(themeAdapter);
        gv_star.setAdapter(starAdapter);
        gv_price.setAdapter(priceAdapter);
        gv_score.setAdapter(scoreAdapter);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleMessageEvent(DafultMessageEvent message) {
        switch (message.TAG) {

            case "PositionFilterActivity":

                position = message.message;

                tv_position.setText(position);

                id = message.belong;

                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }
}
